package top.lshaci.learning.springboot.web.controller;

import cn.hutool.core.io.IoUtil;
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver;
import org.springframework.web.util.UriUtils;
import top.lshaci.learning.springboot.web.model.FileAndParam;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * FileUploadController
 *
 * @author lshaci
 * @since 1.0.0
 */
@Controller
@RequestMapping("/file")
public class FileUploadController {
    /**
     * 文件上传配置：{@link MultipartAutoConfiguration}
     * 1.自动配置文件上传解析器 multipartResolver
     * 2.原理步骤
     * 1）使用文件上传解析器判断（isMultipart）并封装(resolveMultipart ==> {@link StandardMultipartHttpServletRequest})文件上传请求
     * 2）使用 {@link RequestPartMethodArgumentResolver} 解析请求中的文件参数，封装为 {@link MultipartFile}
     */

    @PostMapping("/upload")
    public Object upload(
            @RequestParam("email") String email,
            @RequestParam("username") String username,
            @RequestPart("headImg") MultipartFile headImg,
            @RequestPart("photos") MultipartFile[] photos
    ) throws IOException {
        if (!headImg.isEmpty()) {
            headImg.transferTo(new File("./", headImg.getOriginalFilename()));
        }

        return null;
    }

    @PostMapping(value = "/uploadAndParam", consumes = "multipart/form-data;charset=UTF-8")
    public Object uploadAndParam(FileAndParam fileAndParam) throws IOException {
        System.err.println(fileAndParam);
        return null;
    }


    @GetMapping("/downloadDispatcher")
    public void downloadDispatcher(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        RequestDispatcher dispatcher = request.getRequestDispatcher("/file/download");
        RequestDispatcher dispatcher = request.getRequestDispatcher("/fileDownload/项目台账.xlsx");
        String downloadName = UriUtils.encode("项目台账.xlsx", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + downloadName);
        dispatcher.forward(request, response);
    }

    @GetMapping("/download")
    public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {
        File file = new File("D:/", "test.jpg");
        FileInputStream fis = new FileInputStream(file);
        /*
         * 解决特殊字符不能下载问题
         * 1、使用org.springframework.web.util.UriUtils对文件名进行UTF-8编码
         * 2、response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + downloadName)
         */
        String downloadName = UriUtils.encode("test.jpg", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + downloadName);
        IoUtil.copy(fis, response.getOutputStream());
    }
}
